package br.uefs.dexa.rfframework.db.map;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;

import br.uefs.dexa.rfframework.config.AppConfigManager;
import br.uefs.dexa.rfframework.db.DatabaseManager;
import br.uefs.dexa.rfframework.db.annotation.AnnotationFormatter;
import br.uefs.dexa.rfframework.db.annotation.SimpleLineAnnotationFormatter;

public class MapDatabaseManager implements DatabaseManager {
	
	private static MapDatabaseManager instance;
	
	
	private Map<ObjectItem, Properties> mapFiles = new HashMap<ObjectItem, Properties>();
	
	private Map<String, String> idFromImagePathMap = new HashMap<String, String>();

	public enum ObjectItem {
		
		IMAGE, ANNOTATION
	}
	

	
	
	public MapDatabaseManager()
	{
		createMaps();
	}
	
	private void createMaps()
	{
		createImageMap();
		createInverseImageMap();
		createAnnotationMap();
	}
	
	private void createImageMap()
	{
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(AppConfigManager.getInstance().getImageMapPath()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		mapFiles.put(ObjectItem.IMAGE, prop);
	}
	
	private void createInverseImageMap()
	{
		Properties props = mapFiles.get(ObjectItem.IMAGE);
		 Enumeration e = props.propertyNames();

		  while (e.hasMoreElements())
		 {
		     String key = (String) e.nextElement();
		     idFromImagePathMap.put(AppConfigManager.getInstance().getImageDirectoryPath() + props.getProperty(key), key);
		 }
	}
	
	private void createAnnotationMap()
	{
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(AppConfigManager.getInstance().getAnnotationMapPath()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		mapFiles.put(ObjectItem.ANNOTATION, prop);
	}
	
	@Override
	public ArrayList<String> getImagePaths()
	{
		ArrayList<String> paths = new ArrayList<String>();
		
		for ( Map.Entry<String, String> entry : idFromImagePathMap.entrySet()) {
		    String key = entry.getKey();
		    paths.add(key);
		}
		return paths;
	}
	
	@Override
	public long getIdFromImagePath(String imagePath)
	{
		return Long.parseLong(idFromImagePathMap.get(imagePath));
	}
	
	@Override
	public String getImagePath(long objectId) 
	{
		return AppConfigManager.getInstance().getImageDirectoryPath() + mapFiles.get(ObjectItem.IMAGE).getProperty(objectId + "");
	}

	@Override
	public String getAnnotationPath(long objectId) 
	{
		return AppConfigManager.getInstance().getAnnotationDirectoryPath() + mapFiles.get(ObjectItem.ANNOTATION).getProperty(objectId + "");
	}
	
	@Override
	public String getAnnotation(long objectId, AnnotationFormatter af)
	{	
		if(af==null)
			af = new SimpleLineAnnotationFormatter();
		
		return af.format(getAnnotationPath(objectId));
	}
	
	public static MapDatabaseManager getInstance()
	{
		if (instance==null)
			instance = new MapDatabaseManager();
		return instance;
			
	}

}
